package is.ru.honn.rumblr.data;

import is.ru.honn.rumblr.domain.User;
import is.ruframework.data.RuData;
import is.ruframework.data.RuDataAccessException;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

import java.util.*;

/**
 * @author Stefan Gunnarsson
 * @Version 0.1 aplha
 */
public class UserData extends RuData implements UserDataGateway
{
    //Adds a new user into the usertable
    public void addUser(User user) throws  RuDataAccessException
    {
        //defines the insert command
        SimpleJdbcInsert insertUser = new SimpleJdbcInsert(getDataSource()).withTableName("users");

        Map<String, Object> userParameters = new HashMap<String, Object>(2);
        try
        {
            userParameters.put("username", user.getUsername()); //places the right info into the parameter
            userParameters.put("name", user.getName());
            insertUser.execute(userParameters);     //executes the insertcommand with the parameters
        }
        catch (Exception ex)
        {

            throw new RuDataAccessException(ex.getMessage());
        }

    }
    //Searches for a user in the users table and returns it.
    public User getUser(String username)
    {
        SimpleJdbcTemplate queryUser = new SimpleJdbcTemplate(getDataSource());
        List<User> user = queryUser.query("select * from users where username = ? ",new UserRowMapper(), username);
        return user.get(0); //To make sure that it only returns one user
    }

    //Very similar function as above but we are searching in the follows row now
    public List<User> getUsersThatFollowMe(String username)
    {
        SimpleJdbcTemplate queryFollowers = new SimpleJdbcTemplate(getDataSource());
        //Sql search function that populates a userlist
        List<User> followers = queryFollowers.query("select * from followers where follower = ?",new UserRowMapper(), username);
        return followers;
    }

        //Gets all the users that this user is following
    public List<User> getUsersThatIFollow(String username)
    {
        SimpleJdbcTemplate queryFollowers = new SimpleJdbcTemplate(getDataSource());
        //Sql search function that populates a userlist
        List<User> usersIFollow = queryFollowers.query("select * from followers where follower = ?",new UserRowMapper(), username);
        return usersIFollow;
    }

    //Adds a follower to a user.
    public void addFollower(User follower, User follows)
    {
        SimpleJdbcInsert insertFollower = new SimpleJdbcInsert(getDataSource()).withTableName("followers");
        Map<String, Object> followerParameters = new HashMap<String, Object>(2);
        followerParameters.put("follower", follower.getUsername()); //Places the right info into the parameters
        followerParameters.put("follows", follows.getUsername());
        insertFollower.execute(followerParameters);   //Inserts the data into the follower table
    }
}
